import {
	defineStore
} from 'pinia'

import {
	req
} from '@/common/vmeitime-http/'
import api from '@/common/api.js'
export const userStore = defineStore("userInfo", {
	// persist: {
	// 	enabled: true,
	// 	strategies: [
	// 		{
	// 			key: 'userInfoStorage', 
	// 			storage: localStorage,
	// 		},
	// 	],
	// },
	unistorage: true, // 开启后对 state 的数据读写都将持久化
	state: () => ({
		userInfo: {},
		userFlag: '',
		jobList:[],
		addressList:[],
		addressListCheckData:[], //没有不限
		ZpClassifyList:[], //此数据暂时没用到，先留着
		meiyeRecruitJob3List:[], //发布的职位
		
		// businessInfo: {},
		// gysInfo: {}
		//对方的id
		dfid:''
	}),
	getters: {
		getUserFlag: (state) => state.userFlag,
		getUserInfo: (state) => state.userInfo,
		getJobList:(state)=>state.jobList,
		getAddressList:(state)=>state.addressList,
		getAddressListCheckData:(state)=>state.addressListCheckData,
		getZpClassifyList:(state)=>state.ZpClassifyList,
		getMeiyeRecruitJob3List:(state)=>state.meiyeRecruitJob3List
	},
	actions: {
		updateUserInfo(id) {
			//根据token获取最新的用户信息
			return new Promise((resolve, reject) => {
				req('GET', api.updateUserInfo, {
					userId: id
				}).then((res) => {
					console.log(res);
					this.userInfo = res.result;
					resolve(res)
				}).catch((err) => {
					reject(err)
				})
			})
		},
		doLogin(info, flag) {
			//登录设置用户信息
			console.log(info, flag, '仓库收到的的userinfo');
			this.userInfo = info;
			this.userFlag = flag;
		},
		loginOut() {
			//登出
			return new Promise((resolve, reject) => {
				this.userInfo = {}
				this.userFlag = ''
				uni.removeStorageSync('roleFlag')
				uni.removeStorageSync('userInfo')

				uni.removeStorageSync('userFlag')
				resolve(this.userInfo)
				reject('')
			})
		},
		doImLogin(info) {
			console.log('imlogin', info);
			this.imInfo = info;
		},
		// 获取职位
		getAllJobListFun() {
			return new Promise((resolve, reject) => {
				req('GET', api.meiyeRecruitCategoryCommonJobList).then((res) => {
					console.log(res);
					let obj = {
						name:'不限',
						id:'',
						children:[]
					}
					res.result.unshift(obj)
					this.jobList = res.result;
					resolve(res)
				}).catch((err) => {
					reject(err)
				})
			})
		},
		// 获取地区
		getAllAddressTreeFun() {
			return new Promise((resolve, reject) => {
				req('GET', api.getAddressTree).then((res) => {
					let obj = {
						name:'不限',
						id:'',
						children:[]
					}
					let newAddressList = JSON.parse(JSON.stringify(res.result))
					newAddressList.unshift(obj)
					
					this.addressList = Object.freeze(newAddressList);
					this.addressListCheckData = Object.freeze(res.result);
				
					resolve(res)
				}).catch((err) => {
					reject(err)
				})
			})
		},
		// 获取招聘分类
		getZpClassifyFun(id) {
			return new Promise((resolve, reject) => {
				req('GET', api.meiyeRecruitCategory2CategoryList2,{id}).then((res) => {
					let obj = {
						name:'不限',
						id:'',
						children:[]
					}
					res.result.unshift(obj)
					this.ZpClassifyList = res.result;
					resolve(res.result)
				}).catch((err) => {
					reject(err)
				})
			})
		},
		//获取职位管理列表
		meiyeRecruitJob3ListFun() {
			let params = {
				status:'',
				pageNo:1,
				pageSize:999,
			}
			return new Promise((resolve, reject) => {
				req('GET', api.meiyeRecruitJob3List,params).then((res) => {
					// let obj = {
					// 	jobName:'不限',
					// 	id:'',
					// 	children:[]
					// }
					// res.result.records.unshift(obj)
					console.log(res.result.records,'res.result.records')
					this.meiyeRecruitJob3List = res.result.records;
					// this.ZpClassifyList = res.result;
					resolve(res.result.records)
				}).catch((err) => {
					reject(err)
				})
			})
		},
		//更新对方id
		updatadfid(id){
			this.dfid = id
		}
	},
})